//
//  HEIMVideoCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/24.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEIMVideoCell.h"
#import "HEIMVideoObject.h"
#import "KNPhotoBrowser.h"

@interface HEIMVideoCell ()<KNPhotoBrowserDelegate>

@property (nonatomic, strong) UIImageView *videoView;

@property (nonatomic,weak) KNPhotoBrowser *photoBrowser;
@property (nonatomic,strong) NSMutableArray *itemsArr;
@property (nonatomic,strong) KNPhotoItems *items;

@end

@implementation HEIMVideoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
   if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
       self.selectionStyle = UITableViewCellSelectionStyleNone;
 
       [self initUI];
   }
   return self;
}

- (void)initUI {
    
    _videoView = [UIImageView new];
    _videoView.contentMode = UIViewContentModeScaleAspectFill;
    _videoView.clipsToBounds = YES;
//    _videoView.image = IMAGE_NAMED(@"noImage");
    _videoView.backgroundColor = KBlackColor;
    [self.bubbleView addSubview:_videoView];
    _videoView.layer.cornerRadius = 10;
    _videoView.userInteractionEnabled = YES;
    [_videoView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidClick:)]];
    _videoView.tag = 1;
    
    _items = [[KNPhotoItems alloc] init];
    _items.sourceView = _videoView;
    _items.isVideo = true;
    [self.itemsArr addObject:_items];

    UIButton *_playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _playBtn.userInteractionEnabled = NO;
    [_playBtn setImage:[UIImage imageNamed:@"new_allPlay_44x44_"] forState:UIControlStateNormal];
    [_videoView addSubview:_playBtn];
    
    [_playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(_videoView);
        make.width.height.mas_equalTo(_videoView.mas_width).multipliedBy(0.4);
    }];
    
    [_videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bubbleView).mas_offset(0);
        make.left.equalTo(self.bubbleView).mas_offset(0);
        make.right.equalTo(self.bubbleView).mas_offset(-0);
        make.bottom.equalTo(self.bubbleView).mas_offset(-0);
    }];
}

- (void)configCellWithModel:(HEIMMessageModel *)model {

    HEIMVideoObject *video = (HEIMVideoObject *)model.message.messageObject;
    _items.url = video.url.absoluteString;
            
    [_videoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(model.contentWidth);
        make.height.mas_offset(model.contentWidth * 1.4);
    }];
    
    self.videoView.image = video.coverImage;

    
    // 必须写
    [self refreshData:model];
}

- (NSMutableArray *)itemsArr{
    if (!_itemsArr) {
        _itemsArr = [NSMutableArray array];
    }
    return _itemsArr;
}

- (void)imageViewDidClick:(UITapGestureRecognizer *)tap{
    KNPhotoBrowser *photoBrowser = [[KNPhotoBrowser alloc] init];
    photoBrowser.itemsArr = [self.itemsArr copy];
//    photoBrowser.isNeedPageControl = true; // if it has video to play , I do not suggest you to use PageControl
    photoBrowser.isNeedPageNumView = true;
    photoBrowser.isNeedRightTopBtn = true;
    photoBrowser.isNeedPictureLongPress = true;
    photoBrowser.isNeedPanGesture = true;
    photoBrowser.isNeedPrefetch = true;
    photoBrowser.isNeedAutoPlay = true;
    
    photoBrowser.currentIndex = tap.view.tag - 1;
    photoBrowser.delegate = self;
    [photoBrowser present];
    
    _photoBrowser = photoBrowser;
}

@end
